/**
 * @ author: 汪孟苹
 * ps：该文件方法不要直接使用，请使用bbcare里对应的方法。（bbcare里的方法只是套了个壳子）
 */
define(["jquery"],function($){
	var simple = {};
	
	/**
	 * 生成n位随机数(默认8位随机数)
	 * @param  {[num]} n [生成随机数的位数]
	 * @return {[num]}   [生成的随机数]
	 */
	simple.random = function(n){
		if( !n ) n = 8;
		return Math.floor(Math.random()*Math.pow(10,n));
	}

	/**
	 * 格式化年龄
	 * 入参 总天数 days
	 * 1、小于三个月的显示几周几天，(days<3*30)
	 * 2、小于1岁的显示几月龄几天，	(3*30<=days<1*365)
	 * 3、大于一岁的显示几岁几月龄，(1*365<=days<6*365)
	 * 4、大于六岁的只显示岁。		(days>=6*365)
	 */
	var formatAge = function(days){
		var age = "";
		if(days < 3*30){
			var week = parseInt(days / 7);
			var day = parseInt(days % 7);
			if(week != 0) age += week + "周";
			age += day + "天";
		}else if(days < 1*365){
			var month = parseInt(days / 30);
			var day = parseInt(days % 30);
			if(month != 0) age += month + "月龄";
			if(day != 0) age += day + "天";
		}else if(days < 6*365){
			var year = parseInt(days / 365);
			var month = parseInt(days % 365 / 30);
			if(year != 0) age += year + "岁";
			if(month != 0) age += month + "月龄";
		}else{
			var year = parseInt(days / 365);
			if(year != 0) age += year + "岁";
		}
		return age;
	}
	simple.formatAge = function(days){
		return formatAge(days);
	}

	/**
	 * 两个日期之间的差 (date2 - date1)
	 * @param  {[type]} date1 [日期1]
	 * @param  {[type]} date2 [日期2]
	 * @return {[object]}       [返回一个对象, object.days是天数; object.age是格式化后的年龄]
	 */
	var dateDiff = function(date1,date2){
		var date1 = new Date(date1);
		var date2 = new Date(date2);
		var s1 = date1.getTime();
		var s2 = date2.getTime();
		var total = (s2 - s1)/1000;
		var days = parseInt(total / (24*60*60)); // 两日期之间的差（天数）
		return {
			days:days,
			age:formatAge(days)
		};
	}
	simple.dateDiff = function(date1,date2){
		return dateDiff(date1,date2);
	}

	/**
	 * 根据出生日期、预产期计算孕周
	 * @param  {[type]} date1 [出生日期]
	 * @param  {[type]} date2 [预产期]
	 * @return {[object]}       [返回一个对象, object.days是天数; object.age是格式化后的孕周]
	 */
	simple.getGestation = function(date1,date2){
		var daysDiff = dateDiff(date1,date2).days;
		var days = 40*7-daysDiff;
		var week = parseInt(days / 7);
		var day = parseInt(days % 7);
		return {
			days:days,
			string:week + "周" + day + "天",
		};
	}
	/**
	 * 补零
	 * @param  {[num]} s
	 * @return {[string]}
	 */
	var fillZero = function(s){
	    return s < 10 ? '0' + s: s;
	}
	simple.fillZero = function(s){
		return fillZero(s);
	}
	/**
	 * 获取时间（时间格式：xxxx-xx-xx）,根据日期对象
	 * @param  {[type]} date [日期对象]
	 * @return {[object]}  [返回一个对象, object.date如2018-02-03; object.locale如2018-02-03 12::03:03]
	 */
	var getDateFormat = function(date){
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var days = date.getDate(); 
		var hours = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		var dates = year +'-'+ fillZero(month) +"-"+ fillZero(days);
		var locale = dates + ' ' + fillZero(hours) + ":" + fillZero(minutes) + ":" + fillZero(seconds);
		return {
			"date":dates,
			"locale":locale
		};
	}
	simple.getDateFormat = function(date){
		return getDateFormat(date);
	}

	/**
	 * 获取当前时间
	 * @return {[object]}  [返回一个对象, object.date如2018-02-03; object.locale如2018-02-03 12::03:03]
	 */
	simple.getCurTime = function(){
		var myDate = new Date();
		return getDateFormat(myDate);
	}

	/**
	 * 获取数字保留小数的位数
	 */
	simple.getDecimalPlaces = function(value){
		var n; //保留了n位小数
		var index = value.toString().indexOf(".");
        if (index > 0){
        	var length = value.toString().length;
        	n = length-index-1;
        }else{
        	n = 0;
        }
        return n;
	}

	return simple;
});